package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-1-25
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import protobuf.MSPKG_SHOW_TILES_NTF;
	
	import utils.FloatHandler;

	public class BetViewManager extends ViewManager
	{
		private var tableId:UInt64;

		private var betTxtList:Array;
		
		private var chipIconList:Array;

		public function BetViewManager(tableId:UInt64,tableView:MovieClip)
		{
			super();
			this.tableId = tableId;

			this.mainUI = tableView;

			betTxtList = [null, tableView.betValue1_txt, tableView.betValue2_txt, tableView.betValue3_txt, tableView.betValue4_txt, tableView.betValue5_txt, tableView.betValue6_txt];
			chipIconList = [null,tableView.chip1_mc,tableView.chip2_mc,tableView.chip3_mc,tableView.chip4_mc,tableView.chip5_mc,tableView.chip6_mc];

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		/**
		 * 保存本轮下注情况
		 **/
		private function get epicycleBetInfos():Array
		{
			return tableDataModel.epicycleBetInfos;
		}

		private function onAddToStage(event:Event):void
		{
			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, function(event:MajiangEvent):void
			{
				if (tableId.toString() != event.data.tableId.toString())
					return;
				updateView();
			});
			//用户跟牌广播
			dispatcher.addEventListener(MSProto.MSID_CALL_NTF.toString(), function(event:GameEvent):void
			{
				if(event.data != tableId.toString()) return;
				updateView();
			});
			//用户加注广播
			dispatcher.addEventListener(MSProto.MSID_RAISE_NTF.toString(), function(event:GameEvent):void
			{
				if(event.data != tableId.toString()) return;
				updateView();
			});
			//用户过牌广播
			dispatcher.addEventListener(MSProto.MSID_PASS_NTF.toString(), function(event:GameEvent):void
			{
				if(event.data != tableId.toString()) return;
				updateView();
			});
			//用户押注广播
			dispatcher.addEventListener(MSProto.MSID_BET_NTF.toString(), function(event:GameEvent):void
			{
				if(event.data != tableId.toString()) return;
				updateView();
			});
			//从牌堆中发牌
			dispatcher.addEventListener(MSProto.MSID_DIST_TILES_NTF.toString(), function(event:Event):void
			{
				updateView();
			});
			//赢牌的时候show自己的牌
			dispatcher.addEventListener(MSProto.MSID_SHOW_TILES_NTF.toString(), function(event:GameEvent):void
			{
				var pkg:MSPKG_SHOW_TILES_NTF = event.data;
				if(pkg.tableId.toString() != tableId.toString()) return;
				updateView();
			});
			//比赛结束10后清空桌面
			dispatcher.addEventListener(MajiangEvent.CLEAR_TABLE_INFO, function(event:GameEvent):void
			{
				if(tableId.toString() != event.data) return;
				updateView();
			});
		}

		private function removeFromStage(event:Event):void
		{

		}

		/**
		 * 更新界面信息
		 **/
		private function updateView():void
		{
			for (var i:int = 1; i <= 6; i++)
			{
				var position:int = i;
				var chairId:int = tableDataModel.getChairIdByPosition(position);

				var betTxt:TextField = betTxtList[i];
				var chipIcon:MovieClip = chipIconList[i];

				if (epicycleBetInfos[chairId] != null && epicycleBetInfos[chairId].currentBet != null && epicycleBetInfos[chairId].currentBet > 0)
				{
					chipIcon.visible = betTxt.visible = true;
					betTxt.text = FloatHandler.TwoFormatToString(epicycleBetInfos[chairId].currentBet);
				}
				else
				{
					chipIcon.visible = betTxt.visible = false;
				}
			}
		}

	}
}
